/*  -*- mode: c++; coding: utf-8; c-file-style: "stroustrup"; -*-

    Copyright 2008 Asier Aguirre <asier.aguirre@gmail.com>
    This file is part of Intromit.

    Intromit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Intromit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Intromit.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "file_mng.hh"
#include "string.hh"
#include "util.hh"

#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

CLASS_INIT(file_mng); // FILE_MNG CLASS

file_mng::file_mng()
{
    CLASS_CTOR(file_mng);
    ctor();
}

file_mng::~file_mng()
{
    CLASS_DTOR(file_mng);
    dtor();
}

void file_mng::ctor()
{
}

void file_mng::dtor()
{
}

bool file_mng::regular(const char *format, ...)
{
    string *str=new string;
    bool dev;

    // call format
    va_list ap;
    va_start(ap, format);
    char *result=str->format_volatile_va(format, ap);
    va_end(ap);

    dev=regular_nf(result);

    delete str;
    return dev;
}

bool file_mng::regular_nf(const char *filename)
{
    struct stat s;
    if(stat(filename, &s)<0 || !S_ISREG(s.st_mode)) return false; else return true;
}

bool file_mng::directory(const char *format, ...)
{
    string *str=new string;
    bool dev;

    // call format
    va_list ap;
    va_start(ap, format);
    char *result=str->format_volatile_va(format, ap);
    va_end(ap);

    struct stat s;
    if(stat(result, &s)<0 || !S_ISDIR(s.st_mode)) dev=false; else dev=true;

    delete str;
    return dev;
}

int file_mng::hole(int start, const char *init_format_number, const char *end_format, ...)
{
    string *str=new string;

    // call end format
    va_list ap;
    va_start(ap, end_format);
    char *result=str->format_volatile_va(end_format, ap);
    va_end(ap);

    int iter=hole_nf(start, init_format_number, result);

    delete str;
    return iter;
}

int file_mng::hole_nf(int start, const char *init_format_number, const char *end_filename)
{
    int iter=start;
    int n_end_filename=strlen(end_filename);
    while(iter<=9999)
    {
	char init[FILE_INIT];
	memcpy(&init[snprintf(init, FILE_INIT, init_format_number, iter)], end_filename, n_end_filename+1);

	struct stat s;
	if(stat(init, &s)<0)
	{
	    if(errno==ENOTDIR) iter=-1;
	    break;
	}
	iter++;
    }
    return iter;
}

bool file_mng::del(const char *format, ...)
{
    string *str=new string;
    bool dev;

    // call end format
    va_list ap;
    va_start(ap, format);
    char *result=str->format_volatile_va(format, ap);
    va_end(ap);

    dev=del_nf(result);

    delete str;
    return dev;
}

bool file_mng::del_nf(const char *filename)
{
    if(unlink(filename)==-1) return false; else return true;
}
